package com.example.finaltest.history;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.finaltest.R;
import com.example.finaltest.shoppingCart.Goods;
import com.example.finaltest.shoppingCart.ShoppingCartAdapter;
import com.example.finaltest.utils.BaseActivity;
import com.example.finaltest.utils.UtilsLog;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

import static in.srain.cube.views.ptr.util.PtrLocalDisplay.dp2px;


public class HistoryActivity extends BaseActivity {
    ListView listView;
    TextView totalPrice,title;
    LinearLayout orderInfo,empty_shopcart,shareInfo,llCart,bottom;
    ImageView back;
    PtrFrameLayout mPtrFrame;
    Goods goodsBean;
    private Context mcontext;
    private ShoppingCartAdapter adapter;
    private List<Goods.GoodsInfo> childs; //子元素的列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shopping);
        goodsBean= (Goods)getIntent().getSerializableExtra("data");
        initView();
        initPtrFrame();
        initData();
        initEvents();
        setListener();

    }



    private void initView() {
        listView = findViewById(R.id.listView);
        totalPrice = findViewById(R.id.total_price);
        orderInfo= findViewById(R.id.order_info);
        shareInfo = findViewById(R.id.share_info);
        llCart =findViewById(R.id.ll_cart);
        mPtrFrame = findViewById(R.id.mPtrframe);
        empty_shopcart = findViewById(R.id.layout_empty_shopcart);
        title = findViewById(R.id.shoppingcart);
        back = findViewById(R.id.shopping_back);
        bottom = findViewById(R.id.bottom);
        bottom.setVisibility(View.GONE);
        title.setText("历史购买");
    }

    private void initPtrFrame() {
        final PtrClassicDefaultHeader header=new PtrClassicDefaultHeader(this);
        header.setPadding(dp2px(20), dp2px(20), 0, 0);
        mPtrFrame.setHeaderView(header);
        mPtrFrame.addPtrUIHandler(header);
        mPtrFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mPtrFrame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrame.refreshComplete();
                    }
                },2000);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
    }

    private void setListener() {
        back.setOnClickListener(listener);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.shopping_back:
                    finish();
                    break;

            }
        }
    };

    private void initData() {
        mcontext = this;
        childs = new ArrayList<>();
        List<Goods.GoodsInfo> list = goodsBean.getData();
        childs.addAll(list);

    }

    private void initEvents() {
        adapter = new ShoppingCartAdapter(childs,this,1);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int firstVisiablePostion=view.getFirstVisiblePosition();
                int top=-1;
                View firstView=view.getChildAt(firstVisibleItem);
                UtilsLog.i("childCount="+view.getChildCount());//返回的是显示层面上的所包含的子view的个数
                if(firstView!=null){
                    top=firstView.getTop();
                }
                UtilsLog.i("firstVisiableItem="+firstVisibleItem+",fistVisiablePosition="+firstVisiablePostion+",firstView="+firstView+",top="+top);
                if(firstVisibleItem==0&&top==0){
                    mPtrFrame.setEnabled(true);
                }else{
                    mPtrFrame.setEnabled(false);
                }
            }
        });
    }



    public void doUpdate(int childPosition, View showCountView) {
        Goods.GoodsInfo good = (Goods.GoodsInfo) adapter.getItem(childPosition);
        int count = good.getCount();
        UtilsLog.i("进行更新数据，数量" + count + "");
        ((TextView) showCountView).setText(String.valueOf(count));
        adapter.notifyDataSetChanged();


    }





}
